package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.EarMark;
import com.antdesignbackend.backend.entity.SlaughterRecord;
import com.antdesignbackend.backend.entity.entityVo.EntryRecordVo;
import com.antdesignbackend.backend.entity.entityVo.FactoryRecordVo;
import com.antdesignbackend.backend.service.EarMarkService;
import com.antdesignbackend.backend.service.EntryRecordService;
import com.antdesignbackend.backend.service.FactoryRecordService;
import com.antdesignbackend.backend.service.SlaughterRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/factoryRecord")
public class FactoryRecordController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    FactoryRecordService factoryRecordService;
    @Autowired
    SlaughterRecordService slaughterRecordService;
    @Autowired
    EarMarkService earMarkService;
    /**
     *入厂记录
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "records/factoryRecord/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody FactoryRecordVo factoryRecordVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<FactoryRecordVo> factoryRecordVos=new ArrayList<>();
        QueryWrapper<FactoryRecordVo> wrapper =new QueryWrapper<>();
        if(factoryRecordVo.getSourceAddress()!=null&& !factoryRecordVo.getSourceAddress().equals(""))
        {
            wrapper.like("a.sourceAddress",factoryRecordVo.getSourceAddress());
        }
        if (factoryRecordVo.getTimeInterval() != null && !factoryRecordVo.getTimeInterval().equals("")){
            String[] str=factoryRecordVo.getTimeInterval().split(" - ");
            String star=str[0];
            String end=str[1];
            wrapper.between("a.entryDate",star,end);
        }
        wrapper.orderByDesc("a.entryDate");
        wrapper.eq("a.eid",getLoginEnterpriseId());
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=factoryRecordService.count(wrapper);
        result.setTotal(total);
        //再查询
        if(total>=0)
        {
            factoryRecordVos=factoryRecordService.search(wrapper,new Page<>(factoryRecordVo.getPageIndex(),factoryRecordVo.getPageSize()));
        }
        result.setData(factoryRecordVos);
        return result;
    }

    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody FactoryRecordVo factoryRecordVo) throws Exception {
        UpdateWrapper<SlaughterRecord> slaughterRecordUpdateWrapper=new UpdateWrapper<>();
        slaughterRecordUpdateWrapper.set("outState","1").eq("slaughterId",factoryRecordVo.getSlaughterId());
        slaughterRecordService.update(slaughterRecordUpdateWrapper);
        Boolean b=factoryRecordService.save(factoryRecordVo);
        if(b){
            result.setMessage("保存成功！");
            result.setCode("200");
        }else{
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return  result;
    }
    @PostMapping("/tableSearchMe/{eid}")
    @ResponseBody
    public ReturnVO tableSearchMe(@PathVariable("eid") int eid) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<FactoryRecordVo> factoryRecordVos=new ArrayList<>();
        QueryWrapper<FactoryRecordVo> wrapper =new QueryWrapper<>();
        FactoryRecordVo factoryRecordVo=new FactoryRecordVo();
        wrapper.eq("a.eid",eid);
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=factoryRecordService.count(wrapper);
        result.setTotal(total);
        //再查询
        if(total>=0)
        {
            factoryRecordVos=factoryRecordService.search(wrapper,new Page<>(factoryRecordVo.getPageIndex(),factoryRecordVo.getPageSize()));

        }
        result.setData(factoryRecordVos);
        return result;
    }
}
